using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Wrath.Xml;

namespace Wrath.CodeBox
{
    public class TokenClass
    {
        private Regex match = null;

        public string Name = "";

        public bool CaseSensitive
        {
            get
            {
                if ((Match.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)

                    return false;

                return true;
            }
        }

        public Regex Match
        {
            get
            {
                return match;
            }

            set
            {
                match = value;
            }
        }

        public string Pattern
        {
            get
            {
                return match.ToString();
            }

            set
            {
                match = new Regex(value, match.Options);
            }
        }

        public string Xml
        {
            get
            {
                return "  <tokenClass name='" + Element.XmlEncode(Name, false) + "'>\r\n" +
                  "   <pattern>" + Element.XmlEncode(Pattern, false) + "</pattern>\r\n" +
                  "  </tokenClass>";

            }
            set
            {
                try
                {
                    FileParser parser = new FileParser(value);
                    Element tokenElement = parser.FindFirstElement("tokenClass");
                    if (tokenElement != null)
                    {
                        Wrath.Xml.Attribute nameAttribute = tokenElement.FindAttribute("name");
                        if (nameAttribute != null)
                        {
                            Name = nameAttribute.Value;
                        }

                        Element patternElement = tokenElement.FindFirstElement("pattern", false);
                        if (patternElement != null)
                        {
                            Pattern = Element.XmlDecode(patternElement.Value);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Name = "";
                    match = null;
                    throw ex;
                }
            }
        }

        internal TokenClass(string name, Regex match)
        {
            this.Name = name;
            this.match = match;
        }

        internal TokenClass(string name, string pattern, bool isCaseSensitive)
        {
            this.Name = name;
            if (isCaseSensitive)
            {
                this.match = new Regex(pattern);
            }
            else
            {
                match = new Regex(pattern, RegexOptions.IgnoreCase);
            }
        }

        internal TokenClass(string source, bool isCaseSensitive)
        {
            // set the options
            if (isCaseSensitive)
                match = new Regex("");
            else
                match = new Regex("", RegexOptions.IgnoreCase);

            // set the parameters
            Xml = source;
        }

        private TokenClass()
        {
            // forbidden
        }

    }
}
